// pages/login/login.js
var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isHide:true,
    // show:false
    userInfo: null,
    isUser: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.getOpenId()
    var userAgree = false
    var userAgree = wx.getStorageSync('userAgree') || false
    that.setData({
      userAgree
    })
  },
  tipAgree:function(){
    this.setData({
      userAgree:true
    })
  },
  goToUserLicence: function(){
    wx.navigateTo({
      url: '/pages/licence/licence',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
getUserProfile() {
    wx.getUserProfile({
      desc: '使用户得到更好的体验',
      success: (res) => {
        console.log("获取用户信息成功", res)
        let user = res.userInfo
        wx.setStorageSync('user', user)
        this.setData({
          isUser: true,
          userInfo: user
        })
        console.log(this.data.userInfo)
        this.login()
      },
      fail: res => {
        console.log("获取用户信息失败", res)
      }
    })
  },
  manager(){
    wx.navigateTo({
      url: '/pages/manager/manager',
    })
  },
  onShow() {
    this.getUserProfile()
    var user = wx.getStorageSync('user')
    if (user && user.nickName) {
      this.setData({
        isUser: true,
        userInfo: user
      })
    }
  },
  getOpenId(){  //获取用户的openid
    let _this=this;
    wx.login({
      success(res) {
          console.log(res)
            if (res.code) {
            // 发起网络请求,改造成请求本地接口获取openid，规避体验版获取不到的问题
            wx.request({
              url: app.globalData.url+'/wx',
              method:'GET',
              data: {
                  code: res.code    //wx.login登录获取的code值
              },
              success(res) {
                  _this.setData({
                      show:true
                  })
                _this.data.openid=res.data.openid;              
          }

            })
          } else {
            console.log('登录失败！' + res.errMsg)
          }

      },
      fail: res => {
        console.log("获取用户信息失败", res)
      }
    })
  },
  login: function() {
        let that = this;
        console.log(that.data.userInfo.nickName)
        wx.request({
            url: app.globalData.url+'/user/login',
            method: 'POST',
            data: {
                username:that.data.userInfo.nickName,
                id:that.data.openid
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
            },
            success(res) {
                app.globalData.role=res.data.role
                // if(res.data.role==1){
                //     app.globalData.isShow0=true
                //     app.globalData.isShow2=true
                //     app.globalData.isShow4=true
                // }
                // if(res.data.role==2){
                //     app.globalData.isShow1=true
                //     app.globalData.isShow3=true
                //     app.globalData.isShow5=true
                // }
                console.log(res.data.role)
                
                if(res.data.role==4){
                  wx.navigateTo({
                    url: '/pages/admin/admin',
                  })
                }
                console.log(app.globalData.role)
                app.globalData.token=res.data.token
                console.log(app.globalData.token)
            }
        })
        // getApp().globalData.userInfo=e.detail.userInfo
        console.log(that.data.openid)
        that.setData({
            isHide: true
        });
        wx.switchTab({
          url: '/pages/to-do/to-do',
        })
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})